import { createRouter, createWebHistory } from "vue-router";

// 路由路径和组件对应关系
const routes = [
  {
    path: "/",
    name: "home",
    component: () => import("../components/CaseIndex.vue"),
  },
  {
    path: "/tip",
    name: "tip",
    component: () => import("../components/base/ch4/errTip.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../components/base/ch4/errLogin.vue"),
  },
  {
    path: "/car",
    name: "car",
    component: () => import("../components/base/ch6/ShopCars.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    name: "errPage",
    component: () => import("../components/base/ch4/errPage.vue"),
  },
  {
    path: "/error",
    // 1种
    // redirect:"/tip"
    redirect: {
      // 2种
      name: "tip",
      query: {
        from: "redirect",
      },
    },
    // redirect:()=>{  // 3种
    //     switch(roleId){
    //         case 1:
    //         return "/list"
    //         case 2:
    //         return "/user"
    //         case 3:
    //         return "/admin"
    //     }
    // }
  },
  {
    path: "/list",
    name: "list",
    component: () => import("../components/school/SchIndex.vue"),
    children: [
      {
        path: "grade",
        name: "grade",
        component: () => import("../components/school/GradeIndex.vue"),
        children: [
          {
            path: "class",
            name: "class",
            component: () => import("../components/school/ClsIndex.vue"),
          },
        ],
      },
    ],
  },
  {
    path: "/1-2",
    name: "1-2",
    component: () => import("../components/base/ch/1-2.vue"),
  },
  {
    path: "/1-2-1",
    name: "1-2-1",
    component: () => import("../components/base/ch/1-2-1.vue"),
  },
  {
    path: "/1-3",
    name: "1-3",
    component: () => import("../components/base/ch/1-3.vue"),
  },
  {
    path: "/1-4",
    name: "1-4",
    component: () => import("../components/base/ch/1-4.vue"),
  },
  {
    path: "/1-5",
    name: "1-5",
    component: () => import("../components/base/ch/1-5.vue"),
  },
  {
    path: "/1-6",
    name: "1-6",
    component: () => import("../components/base/ch/1-6.vue"),
  },
  {
    path: "/1-7",
    name: "1-7",
    component: () => import("../components/base/ch/1-7.vue"),
  },
  {
    path: "/1-8",
    name: "1-8",
    component: () => import("../components/base/ch/1-8.vue"),
  },
  {
    path: "/1-9",
    name: "1-9",
    component: () => import("../components/base/ch/1-9.vue"),
  },
  {
    path: "/1-10",
    name: "1-10",
    component: () => import("../components/base/ch/1-10.vue"),
  },
  {
    path: "/1-11",
    name: "1-11",
    component: () => import("../components/base/ch/1-11.vue"),
  },
  {
    path: "/1-12",
    name: "1-12",
    component: () => import("../components/base/ch/1-12.vue"),
  },
  {
    path: "/1-13",
    name: "1-13",
    component: () => import("../components/base/ch/1-13.vue"),
  },
  {
    path: "/2-1",
    name: "2-1",
    component: () => import("../components/base/ch2/2-1.vue"),
  },
  {
    path: "/2-2",
    name: "2-2",
    component: () => import("../components/base/ch2/2-2.vue"),
  },
  {
    path: "/2-3",
    name: "2-3",
    component: () => import("../components/base/ch2/2-3.vue"),
  },
  {
    path: "/2-4",
    name: "2-4",
    component: () => import("../components/base/ch2/2-4.vue"),
  },
  {
    path: "/3-1",
    name: "3-1",
    component: () => import("../components/base/ch3/3-1.vue"),
  },
  {
    path: "/3-2",
    name: "3-2",
    component: () => import("../components/data/ShowData.vue"),
  },
  {
    path: "/4-1",
    name: "4-1",
    component: () => import("../components/hooks/4-1.vue"),
  },
  {
    path: "/4-2",
    name: "4-2",
    component: () => import("../components/hooks/4-2.vue"),
  },
  {
    path: "/4-3",
    name: "4-3",
    component: () => import("../components/hooks/4-3.vue"),
  },
  {
    path: "/4-4",
    name: "4-4",
    component: () => import("../components/hooks/4-4.vue"),
  },
  {
    path: "/4-5",
    name: "4-5",
    component: () => import("../components/hooks/4-5.vue"),
  },
  {
    path: "/4-6",
    name: "4-6",
    component: () => import("../components/hooks/4-6.vue"),
  },
  {
    path: "/5-1",
    name: "5-1",
    component: () => import("../components/p-c/pp.vue"),
  },
  {
    path: "/5-2",
    name: "5-2",
    component: () => import("../components/p-c/ParentComp.vue"),
  },
  {
    path: "/5-3",
    name: "5-3",
    component: () => import("../components/slot/IndexComp.vue"),
  },
  {
    path: "/5-4",
    name: "5-4",
    component: () => import("../components/c-c/IndexComp.vue"),
  },
  {
    path: "/5-5",
    name: "5-5",
    component: () => import("../components/p-i/p1/pp.vue"),
  },
  {
    path: "/5-6",
    name: "5-6",
    component: () => import("../components/p-i/p2/pp.vue"),
  },
  {
    path: "/5-7",
    name: "5-7",
    component: () => import("../components/p-i/p3/pp.vue"),
  },
  {
    path: "/5-8",
    name: "5-8",
    component: () => import("../components/p-i/p4/useAttrs.vue"),
  },
  {
    path: "/5-9",
    name: "5-9",
    component: () => import("../components/p-i/p5/useSlots.vue"),
  },
  {
    path: "/6-1",
    name: "6-1",
    component: () => import("../components/base/ch4/6-1.vue"),
  },
  {
    path: "/6-1/:id",
    name: "6-2",
    component: () => import("../components/base/ch4/6-1.vue"),
  },
  {
    path: "/7-1",
    name: "7-1",
    component: () => import("../components/base/ch5/UseStore.vue"),
  },
  {
    path: "/7-2",
    name: "7-2",
    component: () => import("../components/base/ch5/UseList.vue"),
  },
  {
    path: "/8-1",
    name: "8-1",
    component: () => import("../components/base/ch6/ActiveComp.vue"),
  },
  {
    path: "/8-3",
    name: "8-3",
    component: () => import("../components/base/ch6/8-3.vue"),
  },
  {
    path: "/8-4",
    name: "8-4",
    component: () => import("../components/base/ch6/8-4.vue"),
  },
  {
    path: "/8-5",
    name: "8-5",
    component: () => import("../components/base/ch6/8-5.vue"),
  },
  {
    path: "/8-6",
    name: "8-6",
    component: () => import("../components/base/ch6/8-6.vue"),
  },
  {
    path: "/9-1",
    name: "9-1",
    component: () => import("../components/echarts/9-1.vue"),
  },
  // {
  //     path: '/error',
  //     // redirect:"/tip" //1种
  //     redirect:{      //2种
  //         name:"tip",
  //         query:{
  //             from:"redirect"
  //         }
  //     }
  // }
];
// 实例化后的路由对象
const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from) => {
  if (
    to.name == "1-3" &&
    from.name == "1-2" &&
    !localStorage.getItem("login")
  ) {
    return "/login";
  }
});

// 导出配置完的路由对象
export default router;
